const grain_regex = rx=r"(\d+)\s?([a-z]*)"

const grain_dict = Dict(
    "ns"=>Nanoseconds, "nanoseconds"=>Nanoseconds, "nanosecond"=>Nanoseconds,
    "μs"=>Microseconds, "us"=>Microseconds, "microseconds"=>Microseconds, "microsecond"=>Microseconds,
    "ms"=>Milliseconds, "milliseconds"=>Milliseconds, "millisecond"=>Milliseconds,
    "s"=>Seconds, "sc"=>Seconds, "seconds"=>Seconds, "second"=>Seconds,
    "mi"=>Minutes, "minutes"=>Minutes, "minute"=>Minutes,
    "qh"=>QuarterHours, "quarterhours"=>QuarterHours, "quarterhour"=>QuarterHours,
    "hr"=>Hours, "hours"=>Hours, "hour"=>Hours,
    "dy"=>Days, "days"=>Days, "day"=>Days,
    "mo"=>Months, "months"=>Months, "month"=>Month,
    "qy"=>Quarters, "quarters"=>Quarters, "quarter"=>Quarters,
    "yr"=>Years, "years"=>Years, "year"=>Year,
    "dc"=>Decades, "decades"=>Decades, "decade"=>Decades,
    "cn"=>Centuries, "centuries"=>Centuries, "century"=>Centuries
   )

function Base.parse(::Type{T}, str::S) where S<:AbstractString where T<:AbstractGranularUnit 
    str = strip(str)
    matched = match(grain_regex, str)
    if matched == nothing
        throw(ErrorException("\"$(str)\" is not recognized as parseable into type $(T)"))
    end
    value = parse(Int64, matched[1])
    if matched[2] == ""
        return T(value)
    elseif haskey(grain_dict, matched[2])
        T1 = grain_dict[ matched[2] ]
        return T1(value)
    else
        throw(ErrorException("parse($(T), $(str)) is not recognized"))
    end
 end    
 
